const express=require('express');
const pool=require('../pool.js');
const pr=express.Router();

pr.post('/add',(req,res)=>{
    var obj=req.body;
    console.log(obj);
    var i=400;
    for(var k in obj){
        i++;
        if(!obj[k]){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }
    pool.query('insert into xz_index_product set ?',[obj],(err,result)=>{
        if(err){
            res.send({code:500,msg:'服务器端错误'});
            return;
        }
        res.send({code:200,msg:'商品添加成功'});
    })
})

pr.put('/:pid',(req,res)=>{
    var obj1=req.body;
    var obj2=req.params;//路由传参！！！
    console.log(obj1,obj2.pid);
    var i=400;
    for(var k in obj1){
        i++;
        if(!obj1[k]){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }
    pool.query('update xz_index_product set ? where pid=?',[obj1,obj2.pid],(err,result)=>{
        if(err){
            res.send({code:500,msg:'服务器端错误'});
            return;
        }
        console.log(result);
        if(result.affectedRows===0){
            res.send({code:201,msg:'商品修改失败'});
        }else{
            res.send({code:200,msg:'商品修改成功'});
        }
    })
})

module.exports=pr;